<template>
  <a-form-model ref="form" class="form" :model="form.zt" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
    <a-collapse v-model="activeKey">
      <a-collapse-panel key="1" header="总体方案">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-model-item label="模板名称" prop='mb'>
              <a-textarea v-model="form.zt.mb" :rows="1"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计方案名称" prop='name'>
              <a-textarea v-model="form.zt.name" :rows="1"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="类别名称">
              <a-textarea v-model="form.zt.type" :rows="1"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="子类别名称">
              <a-textarea v-model="form.zt.ztype" :rows="1"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="编制日期">
              <j-date style='width: 100%' v-model='form.zt.bzrq'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="编制人">
              <a-textarea v-model="form.zt.bzr" :rows="1" :disabled="true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计开始时间" prop='sjksrq'>
              <j-date style='width: 100%' v-model='form.zt.sjksrq'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计结束时间" prop='sjjsrq'>
              <j-date style='width: 100%' v-model='form.zt.sjjsrq'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计方式" prop='sjfs'>
              <a-textarea v-model="form.zt.sjfs" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="编制依据" prop='bzyj'>
              <a-textarea v-model="form.zt.bzyj" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计目标" prop='sjmb'>
              <a-textarea v-model="form.zt.sjmb" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计范围" prop='sjfw'>
              <a-textarea v-model="form.zt.sjfw" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计内容" prop='sjnr'>
              <a-textarea v-model="form.zt.sjnr" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计组成员分工">
              <a-textarea v-model="form.zt.sjzcyfg" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" class="huanhang">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="被审计单位基本情况">
              <a-textarea v-model="form.zt.bsjdwjbqk" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" class="huanhang">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计范围的风险评估及重要性排序">
              <a-textarea v-model="form.zt.sjfwfxpgj" :rows="5"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="准备阶段开始时间">
              <j-date style='width: 100%' v-model='form.zt.zbjdStart'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="准备阶段结束时间">
              <j-date style='width: 100%' v-model='form.zt.zbjdEnd'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计准备阶段">
              <a-textarea v-model="form.zt.zbjd" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="现场审计开始时间">
              <j-date style='width: 100%' v-model='form.zt.xcsjStart'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="现场审计结束时间">
              <j-date style='width: 100%' v-model='form.zt.xcsjEnd'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="现场审计阶段">
              <a-textarea v-model="form.zt.xcsj" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计报告开始时间">
              <j-date style='width: 100%' v-model='form.zt.sjbgStart'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计报告结束时间">
              <j-date style='width: 100%' v-model='form.zt.sjbgEnd'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计报告阶段">
              <a-textarea v-model="form.zt.sjbg" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="征求意见开始时间">
              <j-date style='width: 100%' v-model='form.zt.zqyjStart'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="征求意见结束时间">
              <j-date style='width: 100%' v-model='form.zt.zqyjEnd'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="征求意见阶段">
              <a-textarea v-model="form.zt.zqyj" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="整改回复开始时间">
              <j-date style='width: 100%' v-model='form.zt.zghfStart'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="整改回复结束时间">
              <j-date style='width: 100%' v-model='form.zt.zghfEnd'></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="整改回复阶段">
              <a-textarea v-model="form.zt.zghf" :rows="3"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-collapse-panel>
      <a-collapse-panel v-if="title!=='' && subView" key="2" :header="title" :disabled="false">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-model-item label="事项名称">
              <a-textarea v-model="form[currentKey].sxmc" :rows="2"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="事项编码">
              <a-textarea v-model="form[currentKey].sxbm" :rows="2"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="事项序号">
              <a-textarea v-model="form[currentKey].sxxh" :rows="2"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="执行人">
              <a-select v-if="zxrShow"
                        v-model="form[currentKey].zxr"
                        @change="handleChange">
                <template v-for="item in zxrList">
                  <a-select-option :value="item.id">
                    {{item.name}}
                  </a-select-option>
                </template>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计目标">
              <a-textarea v-model="form[currentKey].mubiao" :rows="3"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label-col="{span:4}" :wrapper-col="{ span: 20 }" label="审计步骤和方法">
              <a-textarea v-model="form[currentKey].buzhou" :rows="3"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-collapse-panel>
    </a-collapse>
  </a-form-model>
</template>

<script>
  import {getAction, httpAction} from '@/api/manage'
  import store from '@/store/'

  export default {
    name: "tempForm",
    props: ['model', 'templateId', 'id', 'historyTemplateId'],
    data() {
      return {
        title: '',
        subView: true,
        activeKey: ['1', '2'],
        rules: {
          mb: [{required: true, message: '不能为空!'}],
          name: [{required: true, message: '不能为空!'}],
          // sjksrq: [{required: true, message: '不能为空!'}],
          // sjjsrq: [{required: true, message: '不能为空!'}],
          // sjfs: [{required: true, message: '不能为空!'}],
          // bzyj: [{required: true, message: '不能为空!'}],
          // sjmb: [{required: true, message: '不能为空!'}],
          // sjfw: [{required: true, message: '不能为空!'}],
          // sjnr: [{required: true, message: '不能为空!'}],
        },
        labelCol: {span: 8},
        wrapperCol: {span: 16},
        form: {
          zt: {},
        },
        currentKey: '',
        url: {
          details: "/sjxmzygl/sjWorkZtfa/queryById",
          subDetails: "/sjxmzygl/sjWorkZtfaEve/getZtfaEveList",
          add: '/sjxmzygl/sjWorkZtfa/add',
          edit: '/sjxmzygl/sjWorkZtfa/edit',
          zxr: '/sjxmzygl/sjWorkZtfa/getZxrByProId'
        },
        tempForm: {},
        zxrShow: true,
        keyArr: [],
        zxrList: []
      }
    },
    watch: {
      title(val) {
        if (val) {
          this.activeKey = ['2']
        } else {
          this.activeKey = ['1']
        }
      }
    },
    created() {
      this.getZxr()
      this.getDetails()

    },
    methods: {
      initData(data) {
        this.tempForm = data;
      },
      handleChange() {
        this.zxrShow = false
        this.$nextTick(() => {
          this.zxrShow = true
        })
      },
      addKey(path) {
        debugger
        for (let i = 0; i < path.length; i++) {
          let key = 'key' + path[i].treeNo
          if (this.form[key]) {
            this.currentKey = key
          } else {
            this.keyArr.push(key)
            if (i === path.length - 1) {
              this.getSubDetails(path[i].pid, path[i].id, path[i].name, key, path[i].treeNo)
            }
          }
        }
        // for (let item of path) {
        //   let key = 'key' + item.id
        //   if (this.form[key]) {
        //     this.currentKey = key
        //   } else {
        //     this.keyArr.push(key)
        //     this.getSubDetails(item.pid, item.id, item.name, key)
        //   }
        // }
      },
      saveForm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            if (!this.model.id && this.keyArr.length === 0) {
              this.$message.error('请至少选择一个方案')
              return false
            }
            let obj = Object.assign(this.form, {keyArr: this.keyArr})
            let url, type
            if (!this.model.id) {
              url = this.url.add
              type = 'post'
            } else {
              url = this.url.edit
              type = 'put'
            }
            httpAction(url, obj, type).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('saveFormSuccess')
              } else {
                console.log(res)
              }
            })
          } else {
            return false
          }
        })
      },
      getZxr() {
        getAction(this.url.zxr, {proId: this.id}).then((res) => {
          if (res.success) {
            this.zxrList = res.result.records || res.result;
          } else {
            this.zxrList = []
          }
        })
      },
      getDetails() {
        getAction(this.url.details, {id: this.model.id}).then((res) => {
          if (res.success) {
            this.form.zt = res.result.records || res.result;
            this.form.zt.type = this.tempForm.type || this.form.zt.type;
            this.form.zt.ztype = this.tempForm.ztype || this.form.zt.ztype;
            this.form.zt.proid = this.id
            this.form.zt.bzr = store.getters.userInfo.realname
            this.form.zt.templateId = this.templateId
            this.form.zt.mbId = this.historyTemplateId
            console.log(this.historyTemplateId);
            console.log(this.form.zt);
          }
        })
      },
      async getSubDetails(pid, id, name, key, treeNo) {
        this.subView = false
        let res = await getAction(this.url.subDetails, {proId: this.id, ztfaId: this.model.id, treeNo: treeNo})
        if (res.success) {
          this.form[key] = res.result.records || res.result;
          this.subView = true
        } else {
          this.form[key] = {
            pid: pid, sxmc: name, sxbm: '', sxxh: '', zxr: '', mubiao: '', buzhou: '', id: id, treeNo: treeNo
          }
          this.subView = true
        }
        this.$nextTick(() => {
          console.log(this.form);
          this.currentKey = key
        })
      }
    }
  }
</script>

<style scoped>
  .huanhang /deep/ .ant-form-item-label > label {
    overflow: hidden;
    white-space: normal;
    display: inline-block;
    margin-right: 10px;
  }

  .form {
    border-radius: 5px;
    height: calc(100vh - 385px);
    overflow: auto;
  }
</style>